#include "Skybox.h"
#include <string>


Skybox::Skybox()
{
}


Skybox::~Skybox(void)
{
	m_pSkyboxTechnique = NULL;
	m_pCubemapTex = NULL;
}

bool Skybox::init(const char* directory, const char* front, const char* back, const char* left, const char* right, const char* top, const char* bot)
{
	m_pSkyboxTechnique = new SkyboxTechnique();

	if(!m_pSkyboxTechnique->Init())
	{
        std::cout << "Error initializing the skybox technique" << std::endl;
		if (std::cin.get() == '\n')		
			return false;
	}

	m_pSkyboxTechnique->Enable();
	m_pSkyboxTechnique->SetTextureUnit(0);

	m_pCubemapTex = new CubemapTexture(directory, front, back, right, left, top, bot);

	if (!m_pCubemapTex->Load()) 
	{
		std::cout << "m_pCubemapTex->Load failed" << std::endl;
		if (std::cin.get() == '\n')		
			return false;
    }

	Mesh newMesh;
	if(!newMesh.LoadMesh("models/Box.obj"))
	{
		std::cout << "LoadMesh failed" << std::endl;
		if (std::cin.get() == '\n')		
			return false;
	}
	m_Mesh = newMesh;
	return true;
}

void Skybox::render(glm::mat4 ProjectionMatrix, glm::mat4 ViewMatrix, glm::mat4 ModelMatrix)
{
	m_pSkyboxTechnique->Enable();

	GLint OldCullFaceMode;
    glGetIntegerv(GL_CULL_FACE_MODE, &OldCullFaceMode);
    GLint OldDepthFuncMode;
    glGetIntegerv(GL_DEPTH_FUNC, &OldDepthFuncMode);
    
    glCullFace(GL_FRONT);
    glDepthFunc(GL_LEQUAL);

	m_pSkyboxTechnique->SetWVP(ProjectionMatrix);
    
	m_pCubemapTex->Bind(GL_TEXTURE0);
	m_Mesh.Render();
    
    glCullFace(OldCullFaceMode);        
    glDepthFunc(OldDepthFuncMode);
}

